﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using RegOnline.RegOnlineService;

namespace RegOnline
{
    public partial class DobTrebChildWindow : ChildWindow
    {
        public MainViewModel ac;
        public string newText="";
        public RefReq selectedRefreq;

        public DobTrebChildWindow()
        {
            InitializeComponent();
            label1.Visibility = Visibility.Collapsed;
        }

        void ac_InsertRefReqLoaded(object sender, EventArgs e)
        {
            try
            {
                if ((e as AsyncCompletedEventArgs).Error == null) MessageBox.Show("Название требования добавлено в список");
                ac.RefReqLoaded += new EventHandler(ac_refReqLoaded);
                ac.LoadReq();
            }
            catch
            {
            }

        }

        void ac_refReqLoaded(object sender, EventArgs e)
        {
            try
            {
              nameReqComboBox.ItemsSource = ac.RefReqs;
            }
            catch (Exception ex)
            {
                ChildWindow errorWin = new ErrorWindow(ex);
                errorWin.Show();
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.expdocTextBox.Text == string.Empty || this.nameReqComboBox.SelectedIndex == -1)
            {
                label1.Visibility = Visibility.Visible;
            }
            else
            {
                this.DialogResult = true;
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void btAddNewTreb_Click(object sender, RoutedEventArgs e)
        {
            addNewTrebCC TC = new addNewTrebCC();
            TC.Show();
            TC.Closed += new EventHandler(addNewTrebCC_Closed);

        }

        void addNewTrebCC_Closed(object sender, EventArgs e)
        {
            addNewTrebCC dt = (addNewTrebCC)sender;
            
            if (dt.DialogResult==true)
            try
            {
                ac.InsertRefReqAsync(dt.nameReqTextBox.Text);
                ac.RefReqLoaded += new EventHandler(ac_refReqLoaded);
                ac.LoadReq();
            }
            catch (Exception ex)
            {
                ChildWindow errorWin = new ErrorWindow(ex);
                errorWin.Show();
            }
        }

        private void expdocTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            label1.Visibility = Visibility.Collapsed;
            newText = expdocTextBox.Text;
        }

        private void nameReqComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                label1.Visibility = Visibility.Collapsed;
                selectedRefreq = nameReqComboBox.SelectedItem as RefReq;
            }
            catch (Exception )
            {
            }
        }

       
        private void ChildWindow_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                ac.RefReqLoaded += new EventHandler(ac_refReqLoaded);
                ac.LoadReq();
            }
            catch 
            {
            }
               
            //ac.RefReqIserted += new EventHandler(ac_InsertRefReqLoaded);
            //ac.LoadReqWork();
           
        }
    }
}

